package cn.yy.cloudpay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.yy.cloudpay.activity.BaseActivity;
import cn.yy.cloudpay.application.CloudPayApplication;
import cn.yy.cloudpay.listener.ITitlebarClickListener;
import cn.yy.cloudpay.util.Constant;
import cn.yy.cloudpay.view.TitlebarLayout;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class CardAdminActivity extends BaseActivity {

	private Context mContext;
	private CloudPayApplication mApplication;
	private TitlebarLayout titlebar;
	private RelativeLayout relativeLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_card_admin);
		mContext = this;
		mApplication = (CloudPayApplication) getApplication();
		initView();
	}

	private void initView() {
		relativeLayout = (RelativeLayout) findViewById(R.id.include1);
		String title = "刷卡管理";
		List<Map<String, String>> list1 = new ArrayList<Map<String, String>>();
		if(Constant.BANKPAYTYPE_NONGSHAN.equals(mApplication.getOperator().getBankType())){
			title += "(农商)";
			list1.add(getMap("结算"));
		}else if(Constant.BANKPAYTYPE_YINGSHEN.equals(mApplication.getOperator().getBankType())){
			list1.add(getMap("签到"));
	        list1.add(getMap("结算"));
	        list1.add(getMap("重打上一笔"));
	        list1.add(getMap("系统设置"));
		}else{
			Toast.makeText(mContext, "刷卡通道未设置！", Toast.LENGTH_LONG).show();
		}
		titlebar = new TitlebarLayout(mContext, relativeLayout, title);
		
		titlebar.setTitlebarClickListener(new ITitlebarClickListener() {
			@Override
			public void OnRetrunListener(View view) {
				finish();
			}
		});
		
		ListView listview = (ListView) findViewById(R.id.listview);
		listview.setDivider(new ColorDrawable(Color.LTGRAY));  
		listview.setDividerHeight(2); 
		
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, list1, R.layout.listview, new String[]{"item"}, new int[]{R.id.text});
        listview.setAdapter(simpleAdapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	if(Constant.BANKPAYTYPE_NONGSHAN.equals(mApplication.getOperator().getBankType())){
            		 switch (position) {
	                     case 0://结算
	                    	 settlementNS();
	                         break;
	                 }
            	}else if(Constant.BANKPAYTYPE_YINGSHEN.equals(mApplication.getOperator().getBankType())){
            		 switch (position) {
                     case 0://签到
                     	sign();
                         break;
                     case 1://结算
                     	settlement();
                         break;
                     case 2://重打上一笔
                     	reprint();
                         break;
                     case 3://系统设置
                     	settings();
                         break;
                 }
            	}else{
            		Toast.makeText(mContext, "刷卡通道未设置！", Toast.LENGTH_LONG).show();
            	}
            }
        });
		
		/*String[] img_text = { "签到", "结算", "重打上一笔", "系统设置"};
		int[] imgs = { R.drawable.pt_alipay, R.drawable.pt_micropay,R.drawable.pt_card,R.drawable.pt_card};
		gridview=(MyGridView) findViewById(R.id.gridview);
		gridview.setAdapter(new MyGridAdapter(this,img_text,imgs));
		gridview.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				switch(arg2){
				case 0: // 签到
					sign();
					break;
				case 1: // 结算
					settlement();
					break;
				case 2: // 重打上一笔
					reprint();
					break;
				case 3: // 系统设置
					settings();
					break;
				}
			}
		});*/
	}
	
	private void callPay(Intent intent, int transType) {
        intent.setAction("com.ys.smartpos.pay.sdk");
        intent.putExtra("transType", transType);
        startActivityForResult(intent, transType);
    }
	
	private void callPayNS(Intent intent, int transType) {
    	intent.setClassName("com.newland.nxs", "com.newland.payment.ui.activity.MainActivity");
        intent.putExtra("transType", transType);
        startActivityForResult(intent, transType);
    }
	
	//登录系统设置
    public void settings() {
        callPay(new Intent(), 1012);
    }
    
    //签到
    public void sign() {
    	new AlertDialog.Builder(CardAdminActivity.this).setTitle("提示")
			.setMessage("是否签到？")
			.setPositiveButton("取消",new DialogInterface.OnClickListener() {
				@Override  
				public void onClick(DialogInterface dialog, int which) {
					
				}  
			})
			.setNegativeButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent();
			        callPay(intent, 50);
				}
			}).show();
    }
    
    //结算
    public void settlement() {
    	new AlertDialog.Builder(CardAdminActivity.this).setTitle("提示")
			.setMessage("是否结算？")
			.setPositiveButton("取消",new DialogInterface.OnClickListener() {
				@Override  
				public void onClick(DialogInterface dialog, int which) {
					 
				}  
			})
			.setNegativeButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					callPay(new Intent(), 54);
				}
			}).show();
    }
    
    //结算（农商）
    public void settlementNS(){
    	new AlertDialog.Builder(CardAdminActivity.this).setTitle("提示")
		.setMessage("是否结算？")
		.setPositiveButton("取消",new DialogInterface.OnClickListener() {
			@Override  
			public void onClick(DialogInterface dialog, int which) {
				 
			}  
		})
		.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				callPayNS(new Intent(), 54);
			}
		}).show();
    }
    
    // 重打上一笔
    private void reprint(){
    	new AlertDialog.Builder(CardAdminActivity.this).setTitle("提示")
		.setMessage("是否重打上一笔？")
		.setPositiveButton("取消",new DialogInterface.OnClickListener() {
			@Override  
			public void onClick(DialogInterface dialog, int which) {
			}  
		})
		.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				callPay(new Intent(), 2008);
			}
		}).show();
    }
    
    private void toast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(CardAdminActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
    
    private Map<String, String> getMap(String string) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("item", string);
        return map;
    }
	
	@Override
	protected void onResume() {
		super.onResume();
	}
}